import { defineStore } from "pinia";
import { CommonHmiDataAPI, PageResult } from "@/api/hmi/common-hmi-data.api";
import { store } from "@/store";

/**
 * 通用 HMI 数据 Store
 */
export const useCommonApiStore = defineStore("commonApi", () => {
  /**
   * 分页查询
   * @param apiPath API 路径
   * @param queryParams 查询参数
   * @returns Promise<PageResult<any[]>>
   */
  async function fetchList(
    apiPath: string,
    queryParams: Record<string, any>
  ): Promise<PageResult<any[]>> {
    const res = await CommonHmiDataAPI.getOffsetList(apiPath, queryParams);
    return { list: res.list, total: res.total };
  }

  /**
   * 批量删除
   * @param apiPath API 路径
   * @param ids ID 数组
   * @returns Promise<void>
   */
  async function batchDelete(apiPath: string, ids: (string | number)[]): Promise<void> {
    await CommonHmiDataAPI.batchDelete(apiPath, ids);
  }

  /**
   * 批量重置
   * @param apiPath API 路径
   * @param ids ID 数组
   * @returns Promise<void>
   */
  async function batchReset(apiPath: string, ids: (string | number)[]): Promise<void> {
    await CommonHmiDataAPI.batchReset(apiPath, ids);
  }

  /**
   * 批量修改
   * @param apiPath API 路径
   * @param data 数据数组
   * @returns Promise<void>
   */
  async function batchUpdate(apiPath: string, data: any[]): Promise<void> {
    await CommonHmiDataAPI.batchUpdate(apiPath, data);
  }

  return {
    fetchList,
    batchDelete,
    batchReset,
    batchUpdate,
  };
});

/**
 * 适用于 setup 之外的场景
 */
export function useCommonApiStoreHook() {
  return useCommonApiStore(store);
}
